<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: dark)"><meta name="generator" content="Hexo 6.3.0">
<link rel="preconnect" href="https://cdnjs.cloudflare.com" crossorigin>
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css" integrity="sha256-CTSx/A06dm1B063156EVh15m6Y67pAjZZaQc89LLSrU=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.24/fancybox/fancybox.css" integrity="sha256-vQkngPS8jiHHH0I6ABTZroZk8NPZ7b+MUReOFE9UsXQ=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/pink/pace-theme-flash.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"fansirsqi.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":true,"version":"8.18.2","exturl":true,"sidebar":{"position":"left","width":210,"display":"always","padding":3,"offset":36},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":500},"bookmark":{"enable":false,"color":"#b94242","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":true,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>



<link rel="canonical" href="http://fansirsqi.github.io/2022/05/07/howtoshell/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://fansirsqi.github.io/2022/05/07/howtoshell/","path":"2022/05/07/howtoshell/","title":"Shell 命令学习"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Shell 命令学习 | 依旧归七</title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<link rel="alternate" href="/atom.xml" title="依旧归七" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">依旧归七</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">Byseven</p>
      <script src="https://v1.hitokoto.cn/?encode=js&select=%23hitokoto" defer></script>
      <p id="hitokoto">
        <a href="#" id="hitokoto_text">✨...✨</a>
      </p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">25</span></a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">85</span></a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">49</span></a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#How-To-Shell"><span class="nav-text">How To Shell ?</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#if-%E5%88%A4%E6%96%AD"><span class="nav-text">if 判断</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#case-%E8%B7%B3%E8%BD%AC"><span class="nav-text">case 跳转</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#read-%E8%AF%BB%E5%8F%96"><span class="nav-text">read 读取</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#echo-%E8%BE%93%E5%87%BA"><span class="nav-text">echo 输出</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#exit-1-%E9%9D%9E%E9%9B%B6%E7%8A%B6%E6%80%81-yi%E2%80%99chang-%E9%80%80%E5%87%BA"><span class="nav-text">exit 1 非零状态[yi’chang]退出</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Fansir"
      src="/images/cavatar.gif">
  <p class="site-author-name" itemprop="name">Fansir</p>
  <div class="site-description" itemprop="description">一个小博客</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">49</span>
          <span class="site-state-item-name">文章</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">25</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">85</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2ZhbnNpcnNxaQ==" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;fansirsqi"><i class="fab fa-github fa-fw"></i></span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="bWFpbHRvOmZhbnNpcmxpdUAxNjMuY29t" title="163-Mail → mailto:fansirliu@163.com"><i class="fa fa-regular fa-envelope fa-fw"></i></span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9zcGFjZS5iaWxpYmlsaS5jb20vNjEzMTU1Njk0" title="bilibili → https:&#x2F;&#x2F;space.bilibili.com&#x2F;613155694"><i class="fab fa-bilibili fa-fw"></i></span>
      </span>
  </div>
  <div class="cc-license animated" itemprop="license">
    <span class="exturl cc-opacity" data-url="aHR0cHM6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL2xpY2Vuc2VzL2J5LW5jLXNhLzQuMC9kZWVkLnpoLUNO"><img src="https://cdnjs.cloudflare.com/ajax/libs/creativecommons-vocabulary/2020.11.3/assets/license_badges/small/by_nc_sa.svg" alt="Creative Commons"></span>
  </div>

        </div>
      </div>
    </div>

    
    <div class="sidebar-inner sidebar-blogroll">
      <div class="links-of-blogroll animated">
        <div class="links-of-blogroll-title"><i class="fa fa-globe fa-fw"></i>
          ✨魔法链接✨
        </div>
        <ul class="links-of-blogroll-list">
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly9nZWVrLnJyYWJpdHMuY29tLyMvcmVnaXN0ZXI/Y29kZT1hQ3B3VUpoRQ==" title="https:&#x2F;&#x2F;geek.rrabits.com&#x2F;#&#x2F;register?code&#x3D;aCpwUJhE">Rrabits-Cloudgeek</span>
            </li>
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly9zYWt1cmFjYXQud29ybGQvIy9yZWdpc3Rlcj9jb2RlPWFXQVVZblhy" title="https:&#x2F;&#x2F;sakuracat.world&#x2F;#&#x2F;register?code&#x3D;aWAUYnXr">SakuraCat</span>
            </li>
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly9zb2NrYm9vbS5sb3ZlL2F1dGgvcmVnaXN0ZXI/YWZmaWQ9MTEwNTMz" title="https:&#x2F;&#x2F;sockboom.love&#x2F;auth&#x2F;register?affid&#x3D;110533">SockBoom</span>
            </li>
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly9zYXVydXJ1cy5pbmsvIy9yZWdpc3Rlcj9jb2RlPW5Ka2ROQldy" title="https:&#x2F;&#x2F;saururus.ink&#x2F;#&#x2F;register?code&#x3D;nJkdNBWr">Saururus</span>
            </li>
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly9ndW9saWNoZW5nLmNjL2luZGV4LnBocCMvcmVnaXN0ZXI/Y29kZT16c21ZSUpudg==" title="https:&#x2F;&#x2F;guolicheng.cc&#x2F;index.php#&#x2F;register?code&#x3D;zsmYIJnv">果粒橙</span>
            </li>
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly94bi0tbWVzcjhiMzZ4LmNvbS8jL3JlZ2lzdGVyP2NvZGU9RG9OSnhkbWk=" title="https:&#x2F;&#x2F;xn--mesr8b36x.com&#x2F;#&#x2F;register?code&#x3D;DoNJxdmi">Big-Airport</span>
            </li>
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly94bi0tbWVzMzU4YWJ5MmFwZmcuY29tLyMvcmVnaXN0ZXI/Y29kZT10aENJNnMxWQ==" title="https:&#x2F;&#x2F;xn--mes358aby2apfg.com&#x2F;#&#x2F;register?code&#x3D;thCI6s1Y">赔钱机场</span>
            </li>
            <li class="links-of-blogroll-item">
              <span class="exturl" data-url="aHR0cHM6Ly93d3cuZWVqc3EubmV0LyMvcmVnaXN0ZXI/Y29kZT1qcWFkR05MMg==" title="https:&#x2F;&#x2F;www.eejsq.net&#x2F;#&#x2F;register?code&#x3D;jqadGNL2">EEVPN</span>
            </li>
        </ul>
      </div>
    </div>
        <div class="pjax">
        </div>
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://fansirsqi.github.io/2022/05/07/howtoshell/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/cavatar.gif">
      <meta itemprop="name" content="Fansir">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="依旧归七">
      <meta itemprop="description" content="一个小博客">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="Shell 命令学习 | 依旧归七">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Shell 命令学习
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-05-07 23:57:08" itemprop="dateCreated datePublished" datetime="2022-05-07T23:57:08+08:00">2022-05-07</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2023-11-16 15:44:56" itemprop="dateModified" datetime="2023-11-16T15:44:56+08:00">2023-11-16</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Linux/" itemprop="url" rel="index"><span itemprop="name">Linux</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>8 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><h1 id="How-To-Shell"><a href="#How-To-Shell" class="headerlink" title="How To Shell ?"></a>How To Shell ?</h1><h2 id="if-判断"><a href="#if-判断" class="headerlink" title="if 判断"></a><code>if</code> 判断</h2><p>在Shell脚本中，可以使用 <code>if</code>语句进行条件判断。<code>if</code>语句的基本结构如下：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token keyword">if</span> <span class="token punctuation">[</span> condition <span class="token punctuation">]</span>
<span class="token keyword">then</span>
    <span class="token comment"># 执行条件为真时的操作</span>
<span class="token keyword">else</span>
    <span class="token comment"># 执行条件为假时的操作</span>
<span class="token keyword">fi</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<span id="more"></span>

<p>在上述代码中，<code>condition</code>是一个表达式，根据其结果（真或假），决定执行哪个代码块。如果 <code>condition</code>为真，则执行 <code>then</code>块中的代码；如果 <code>condition</code>为假，则执行 <code>else</code>块中的代码（如果有）。</p>
<p>下面是一些常见的条件表达式及其使用方法：</p>
<ol>
<li><p>字符串比较：</p>
<ul>
<li><code>-z string</code>：如果字符串为空，则为真。</li>
<li><code>-n string</code>：如果字符串不为空，则为真。</li>
<li><code>string1 = string2</code>：如果两个字符串相等，则为真。</li>
<li><code>string1 != string2</code>：如果两个字符串不相等，则为真。</li>
</ul>
</li>
<li><p>数值比较：</p>
<ul>
<li><code>num1 -eq num2</code>：如果num1等于num2，则为真。</li>
<li><code>num1 -ne num2</code>：如果num1不等于num2，则为真。</li>
<li><code>num1 -lt num2</code>：如果num1小于num2，则为真。</li>
<li><code>num1 -gt num2</code>：如果num1大于num2，则为真。</li>
<li><code>num1 -le num2</code>：如果num1小于或等于num2，则为真。</li>
<li><code>num1 -ge num2</code>：如果num1大于或等于num2，则为真。</li>
</ul>
</li>
<li><p>文件&#x2F;目录判断：</p>
<ul>
<li><code>-e file</code>：如果文件&#x2F;目录存在，则为真。</li>
<li><code>-f file</code>：如果文件存在且为普通文件，则为真。</li>
<li><code>-d directory</code>：如果目录存在，则为真。</li>
</ul>
</li>
</ol>
<p>下面是一个简单的示例，演示如何在Shell中使用 <code>if</code>语句进行条件判断：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token shebang important">#!/bin/bash</span>

<span class="token assign-left variable">number</span><span class="token operator">=</span><span class="token number">42</span>

<span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token variable">$number</span> <span class="token parameter variable">-eq</span> <span class="token number">42</span> <span class="token punctuation">]</span>
<span class="token keyword">then</span>
    <span class="token builtin class-name">echo</span> <span class="token string">"The number is 42."</span>
<span class="token keyword">else</span>
    <span class="token builtin class-name">echo</span> <span class="token string">"The number is not 42."</span>
<span class="token keyword">fi</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>在上述示例中，如果 <code>$number</code>的值等于42，则输出”The number is 42.”，否则输出”The number is not 42.”。</p>
<p>请注意，Shell中的条件表达式要用方括号 <code>[]</code>括起来，并且每个表达式的左右两侧都要有空格。另外，<code>then</code>和 <code>else</code>之后需要用换行符或分号 <code>;</code>分隔。</p>
<h2 id="case-跳转"><a href="#case-跳转" class="headerlink" title="case 跳转"></a><code>case</code> 跳转</h2><p>在Shell脚本中，<code>case</code>语句用于根据变量的不同取值执行不同的操作。<code>case</code>语句的基本结构如下：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token keyword">case</span> variable <span class="token keyword">in</span>
    pattern1<span class="token punctuation">)</span>
        <span class="token comment"># 如果 variable 匹配 pattern1，则执行对应操作</span>
        <span class="token punctuation">;</span><span class="token punctuation">;</span>
    pattern2<span class="token punctuation">)</span>
        <span class="token comment"># 如果 variable 匹配 pattern2，则执行对应操作</span>
        <span class="token punctuation">;</span><span class="token punctuation">;</span>
    pattern3<span class="token punctuation">)</span>
        <span class="token comment"># 如果 variable 匹配 pattern3，则执行对应操作</span>
        <span class="token punctuation">;</span><span class="token punctuation">;</span>
    *<span class="token punctuation">)</span>
        <span class="token comment"># 如果 variable 不匹配任何 pattern，则执行默认操作</span>
        <span class="token punctuation">;</span><span class="token punctuation">;</span>
<span class="token keyword">esac</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>在上述代码中，<code>variable</code>是需要进行匹配的变量，<code>pattern</code>是要匹配的模式，<code>;;</code>表示一个模式匹配结束，<code>*</code>表示默认情况下的操作。</p>
<p>下面是一个示例，演示如何在Shell中使用 <code>case</code>语句：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token shebang important">#!/bin/bash</span>

<span class="token assign-left variable">fruit</span><span class="token operator">=</span><span class="token string">"apple"</span>

<span class="token keyword">case</span> <span class="token variable">$fruit</span> <span class="token keyword">in</span>
    <span class="token string">"apple"</span><span class="token punctuation">)</span>
        <span class="token builtin class-name">echo</span> <span class="token string">"It's an apple."</span>
        <span class="token punctuation">;</span><span class="token punctuation">;</span>
    <span class="token string">"banana"</span><span class="token punctuation">)</span>
        <span class="token builtin class-name">echo</span> <span class="token string">"It's a banana."</span>
        <span class="token punctuation">;</span><span class="token punctuation">;</span>
    <span class="token string">"orange"</span><span class="token punctuation">)</span>
        <span class="token builtin class-name">echo</span> <span class="token string">"It's an orange."</span>
        <span class="token punctuation">;</span><span class="token punctuation">;</span>
    *<span class="token punctuation">)</span>
        <span class="token builtin class-name">echo</span> <span class="token string">"It's an unknown fruit."</span>
        <span class="token punctuation">;</span><span class="token punctuation">;</span>
<span class="token keyword">esac</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>在上述示例中，根据 <code>$fruit</code>的取值，<code>case</code>语句会匹配相应的模式，并执行对应的操作。如果 <code>$fruit</code>是”apple”，则输出”It’s an apple.”；如果 <code>$fruit</code>是”banana”，则输出”It’s a banana.”；如果 <code>$fruit</code>是”orange”，则输出”It’s an orange.”；如果 <code>$fruit</code>不匹配任何模式，则输出”It’s an unknown fruit.”。</p>
<p>请注意，在每个模式结束时，需要使用 <code>;;</code>表示匹配结束，以确保只执行对应的操作块。最后，<code>esac</code>表示 <code>case</code>语句的结束。</p>
<h2 id="read-读取"><a href="#read-读取" class="headerlink" title="read 读取"></a><code>read</code> 读取</h2><p>在Shell脚本中，<code>read</code>命令用于从标准输入（通常是键盘）读取用户输入，并将输入的值赋给一个或多个变量。<code>read</code>命令的基本语法如下：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token builtin class-name">read</span> <span class="token punctuation">[</span>options<span class="token punctuation">]</span> variable1 variable2 <span class="token punctuation">..</span>.<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>在上述语法中，<code>variable1</code>、<code>variable2</code>等是要接收输入值的变量名。当用户输入完成后，<code>read</code>命令将输入值赋给这些变量。</p>
<p>以下是一些常用的 <code>read</code>命令选项：</p>
<ul>
<li><code>-p prompt</code>：显示一个提示符 <code>prompt</code>，然后等待用户输入。</li>
<li><code>-s</code>：静默模式，用户输入时不显示在屏幕上，用于处理密码等敏感信息。</li>
<li><code>-t timeout</code>：设置超时时间（秒），如果用户在指定的超时时间内未输入，则 <code>read</code>命令会退出。</li>
</ul>
<p>下面是一个简单的示例，演示如何在Shell中使用 <code>read</code>命令：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token shebang important">#!/bin/bash</span>

<span class="token builtin class-name">echo</span> <span class="token string">"Please enter your name:"</span>
<span class="token builtin class-name">read</span> name

<span class="token builtin class-name">echo</span> <span class="token string">"Please enter your age:"</span>
<span class="token builtin class-name">read</span> age

<span class="token builtin class-name">echo</span> <span class="token string">"Hello, <span class="token variable">$name</span>! You are <span class="token variable">$age</span> years old."</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>在上述示例中，首先显示提示信息，要求用户输入姓名。然后，<code>read</code>命令将用户输入的值赋给变量 <code>name</code>。接着，显示另一个提示信息，要求用户输入年龄。再次使用 <code>read</code>命令将用户输入的值赋给变量 <code>age</code>。最后，脚本会输出类似于”Hello, John! You are 30 years old.”的消息，其中 <code>$name</code>和 <code>$age</code>会被用户输入的值替换。</p>
<p>当脚本执行到 <code>read</code>命令时，它会等待用户输入，并在用户按下回车键后继续执行下一条命令。</p>
<h2 id="echo-输出"><a href="#echo-输出" class="headerlink" title="echo 输出"></a><code>echo</code> 输出</h2><p>在Shell脚本中，<code>echo</code>命令用于在终端上输出文本或变量的值。<code>echo</code>命令的基本语法如下：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token builtin class-name">echo</span> <span class="token punctuation">[</span>options<span class="token punctuation">]</span> <span class="token punctuation">[</span>string<span class="token punctuation">]</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>在上述语法中，<code>string</code>是要输出的文本或变量。<code>echo</code>命令会将该文本或变量的值显示在终端上。</p>
<p>以下是一些常用的 <code>echo</code>命令选项：</p>
<ul>
<li><code>-n</code>：不输出行末的换行符，即不换行。</li>
<li><code>-e</code>：启用特殊字符的解析。例如，可以使用 <code>\n</code>表示换行符、<code>\t</code>表示制表符等。</li>
</ul>
<p>下面是一些示例，演示了在Shell中使用 <code>echo</code>命令的不同用法：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token shebang important">#!/bin/bash</span>

<span class="token comment"># 输出文本</span>
<span class="token builtin class-name">echo</span> <span class="token string">"Hello, World!"</span>

<span class="token comment"># 输出变量的值</span>
<span class="token assign-left variable">name</span><span class="token operator">=</span><span class="token string">"John"</span>
<span class="token builtin class-name">echo</span> <span class="token string">"My name is <span class="token variable">$name</span>."</span>

<span class="token comment"># 不换行输出</span>
<span class="token builtin class-name">echo</span> <span class="token parameter variable">-n</span> <span class="token string">"This is "</span>
<span class="token builtin class-name">echo</span> <span class="token string">"a single line."</span>

<span class="token comment"># 解析特殊字符</span>
<span class="token builtin class-name">echo</span> <span class="token parameter variable">-e</span> <span class="token string">"Line 1<span class="token entity" title="\n">\n</span>Line 2<span class="token entity" title="\t">\t</span>Tabbed"</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>在上述示例中，<code>echo</code>命令用于输出不同的信息。第一个 <code>echo</code>语句输出字符串”Hello, World!”。第二个 <code>echo</code>语句输出变量 <code>$name</code>的值，结果为”My name is John.”。第三个 <code>echo</code>语句使用了 <code>-n</code>选项，不会在输出之后换行，而是继续输出下一个 <code>echo</code>语句的内容。最后一个 <code>echo</code>语句使用了 <code>-e</code>选项，并在字符串中使用了特殊字符 <code>\n</code>表示换行和 <code>\t</code>表示制表符，输出两行文本和一个制表符。</p>
<p>需要注意的是，当输出字符串包含变量时，可以使用双引号或不使用引号。双引号将允许解析变量的值，而不使用引号将直接将变量和文本拼接输出。</p>
<p>这就是在Shell中使用 <code>echo</code>命令输出文本或变量的基本方法。如有任何进一步的问题，请随时提问。</p>
<h2 id="exit-1-非零状态-yi’chang-退出"><a href="#exit-1-非零状态-yi’chang-退出" class="headerlink" title="exit 1 非零状态[yi’chang]退出"></a><code>exit 1</code> 非零状态[yi’chang]退出</h2><p>在Shell脚本中，<code>exit 1</code>表示脚本以非零的状态码退出。<code>exit</code>命令用于终止当前正在执行的Shell脚本，并指定一个退出状态码。</p>
<p>在Unix和类Unix系统中，退出状态码是一个用于表示脚本执行结果的整数值。按照约定，如果脚本成功执行并且没有错误，退出状态码为0。非零的退出状态码通常表示脚本执行过程中发生了错误或异常。</p>
<p><code>exit 1</code>表示脚本以状态码1退出，它通常用于指示脚本执行失败或遇到了某种错误情况。状态码可以是从1到255的任何整数值。具体状态码的含义可以根据脚本的需要进行定义和解释。</p>
<p>当脚本执行到 <code>exit 1</code>时，它将立即终止脚本的执行，并将状态码1返回给调用脚本的进程。在命令行中执行脚本时，可以通过特殊变量 <code>$?</code>来获取最后一次执行的命令的退出状态码。</p>
<p>以下是一个示例，展示了在脚本中使用 <code>exit 1</code>的情况：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token shebang important">#!/bin/bash</span>

<span class="token comment"># 检查文件是否存在</span>
<span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token operator">!</span> <span class="token parameter variable">-f</span> <span class="token string">"myfile.txt"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
  <span class="token builtin class-name">echo</span> <span class="token string">"File not found."</span>
  <span class="token builtin class-name">exit</span> <span class="token number">1</span>
<span class="token keyword">fi</span>

<span class="token comment"># 执行其他操作</span>
<span class="token comment"># ...</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>在上述示例中，脚本首先检查文件 <code>myfile.txt</code>是否存在。如果文件不存在，脚本将输出一条错误消息，并使用 <code>exit 1</code>退出状态码1来表示脚本执行失败。</p>
<p>通过使用适当的退出状态码，可以在脚本执行期间检测和处理错误情况，以便在脚本结束后能够了解脚本的执行结果。</p>
<p>希望这个解释对你有帮助。如果还有其他问题，请随时提问。</p>

    </div>

    
    
    

    <footer class="post-footer">
          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>原作者： </strong>归七
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="http://fansirsqi.github.io/2022/05/07/howtoshell/" title="Shell 命令学习">http://fansirsqi.github.io/2022/05/07/howtoshell/</a>
  </li>
  <li class="post-copyright-license">
      <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <span class="exturl" data-url="aHR0cHM6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL2xpY2Vuc2VzL2J5LW5jLXNhLzQuMC9kZWVkLnpoLUNO"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</span> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

          <div class="post-tags">
              <a href="/tags/shell/" rel="tag"><i class="fa fa-tag"></i> shell</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2022/05/07/linux-xi-tong-tong-guo-openssl-ming-ling-sheng-cheng-ssl-zheng-shu/" rel="prev" title="Linux 系统通过openssl命令生成ssl 证书">
                  <i class="fa fa-angle-left"></i> Linux 系统通过openssl命令生成ssl 证书
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2022/05/07/dockerstudy/" rel="next" title="Docker 学习">
                  Docker 学习 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments gitalk-container"></div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2021 – 
    <span itemprop="copyrightYear">2023</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">Fansir</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">55k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">3:20</span>
  </span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>
  <div class="powered-by">由 <span class="exturl" data-url="aHR0cHM6Ly9oZXhvLmlv">Hexo</span> & <span class="exturl" data-url="aHR0cHM6Ly90aGVtZS1uZXh0LmpzLm9yZw==">NexT.Gemini</span> 强力驱动
  </div>

    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.24/fancybox/fancybox.umd.js" integrity="sha256-oyhjPiYRWGXaAt+ny/mTMWOnN1GBoZDUQnzzgC7FRI4=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  
  <script data-pjax async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>




  <script src="https://cdnjs.cloudflare.com/ajax/libs/quicklink/2.3.0/quicklink.umd.js" integrity="sha256-yvJQOINiH9fWemHn0vCA5lsHWJaHs6/ZmO+1Ft04SvM=" crossorigin="anonymous"></script>
  <script class="next-config" data-name="quicklink" type="application/json">{"enable":true,"home":true,"archive":true,"delay":true,"timeout":3000,"priority":true,"url":"http://fansirsqi.github.io/2022/05/07/howtoshell/"}</script>
  <script src="/js/third-party/quicklink.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/gitalk/1.8.0/gitalk.css" integrity="sha256-AJnUHL7dBv6PGaeyPQJcgQPDjt/Hn/PvYZde1iqfp8U=" crossorigin="anonymous">

<script class="next-config" data-name="gitalk" type="application/json">{"enable":true,"github_id":"Fansirsqi","repo":"fansirsqi.github.io","client_id":"242426985831ab7a182d","client_secret":"9f722c40bb448adfa5a0562d4c86eb49aedb4505","admin_user":"Fansirsqi","distraction_free_mode":true,"proxy":"https://cors-anywhere.azm.workers.dev/https://github.com/login/oauth/access_token","language":["zh-CN"],"js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/gitalk/1.8.0/gitalk.min.js","integrity":"sha256-MVK9MGD/XJaGyIghSVrONSnoXoGh3IFxLw0zfvzpxR4="},"path_md5":"85d2f8862a170d4af0eb184d0e2eb060"}</script>
<script src="/js/third-party/comments/gitalk.js"></script>

</body>
</html>
